/*
 * XPreferenceDialog.java, created on Nov 12, 2005
 * 
 * Copyright (c) 2005 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.dialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import org.xfc.XApp;

import com.jgoodies.forms.layout.FormLayout;

// TODO: finish implementing XPreferenceTab support

// TODO: preference tabs should support descriptions, to go at the top of the pref tab

// TODO: listen to property changes in the preference tabs?

// TODO: indicate which tabs are 'dirty'?


/**
 *
 *
 * @author Devon Carew
 */
public class XPreferenceDialog
	extends XDialog
{
	protected List editors = new ArrayList();
	
	private JPanel 		content = new JPanel();
	private JTabbedPane	tabbedPane = new JTabbedPane();
	
	private Action okAction = new AbstractAction() {
		public void actionPerformed(ActionEvent event) { setVisible(false); }
	};
	
	/**
	 * @param app
	 */
	public XPreferenceDialog(XApp app)
	{
		this(app.getActiveAppFrame());
	}

	/**
	 * @param parent
	 */
	public XPreferenceDialog(Frame parent)
	{
		super(parent);
		
		// TODO: have OK do something different then close
		setOKAction(okAction);
		setCancelAction(createDefaultCloseAction());
		
		// TODO: I18N
		setTitle("Preferences");
		
		setMinimumSize(new Dimension(300, 200));
	}
	
	/**
	 * @param preferenceTab
	 */
	public void addPreferenceTab(XPreferenceTab preferenceTab)
	{
		editors.add(preferenceTab);
		
		if (editors.size() == 1)
		{
			content.setLayout(new BorderLayout());
			content.add(centerComponent(preferenceTab.getContent()), BorderLayout.CENTER);
		}
		else if (editors.size() == 2)
		{
			content.removeAll();
			
			tabbedPane = new JTabbedPane();
			content.add(tabbedPane, BorderLayout.CENTER);
			
			addTab((XPreferenceTab)editors.get(0));
			addTab(preferenceTab);
		}
		else
		{
			addTab(preferenceTab);
		}
	}
	
	public JPanel getContents()
	{
		return content;
	}
	
	private void addTab(XPreferenceTab tab)
	{
		JPanel panel = centerComponent(tab.getContent());
		
		panel.setBorder(new EmptyBorder(5, 6, 5, 6));
		
		tabbedPane.addTab(tab.getTitle(), tab.getIcon(), centerComponent(panel));
	}
	
	private JPanel centerComponent(JComponent component)
	{
		JPanel panel = new JPanel(new FormLayout("center:pref", "pref:grow"));
		
		panel.add(component, "1, 1, c, t");
		
		return panel;
	}
	
	/**
	 * Returns an Action that will show this preferences dialog.
	 * 
	 * @return an Action that will show this preferences dialog
	 */
	public Action getShowAction()
	{
		return new AbstractAction() {
			public void actionPerformed(ActionEvent event) {
				setVisible(true);
			}
		};
	}
	
}
